package com.ohuang.apkpatch

import com.google.gson.Gson
import java.io.File

fun main(args: Array<String>) {

    if (args.isEmpty()) {
        println("-a  <config.json 包名替换规则和修改过需要保留的包名>  <smali路径> <输出路径>   生成热更包环境(根据修改的包名生成)")
        println("-b <path>  输出config.json")
        println("-c  <config.json 包名替换规则和修改过需要保留的包名> <smali路径旧包> <smali路径新包> <输出路径>   生成差分热更包环境(保留包名配置不生效)")
        println("-d  <旧包文件路径> <新包文件路径> <输出文件路径>  输出需要更新的文件")
        println("-e  <java.exe 路径(可为空)> <baksmali.jar 路径> <smali.jar路径> <旧包apk> <新包apk> <输出补丁apk路径> 生成补丁包")
        println("-f  <java.exe 路径(可为空)> <baksmali.jar 路径> <smali.jar路径> <旧包apk> <新包apk> <输出补丁apk路径> 生成补丁包(不更新资源)")
        println("-g  <java.exe 路径(可为空)> <baksmali.jar 路径> <smali.jar路径>  <新包apk> <输出补丁apk路径> 生成补丁包(不更新资源,不做差分)")
    } else {
        when (args[0]) {
            "-a" -> {
                //-a  <path1>  <path2>   合并apk反编译环境
                var path1 = args[1].replace("\"", "").replace("'", "")
                var path2 = args[2].replace("\"", "").replace("'", "")
                var path3 = args[3].replace("\"", "").replace("'", "")
                println("path1=$path1")
                println("path2=$path2")
                println("path3=$path3")
                outHotUpdate(path1, path2, path3)
            }

            "-b" -> {
                var path1 = args[1].replace("\"", "").replace("'", "")
                println("path1=$path1")
                outReplaceConfig(path1)
            }

            "-c" -> {
                var path1 = args[1].replace("\"", "").replace("'", "")
                var path2 = args[2].replace("\"", "").replace("'", "")
                var path3 = args[3].replace("\"", "").replace("'", "")
                var path4 = args[4].replace("\"", "").replace("'", "")
                println("path1=$path1")
                println("path2=$path2")
                println("path3=$path3")
                println("path4=$path4")
                outPatch(path1, path2, path3, path4)
            }

            "-d" -> {
                var path1 = args[1].replace("\"", "").replace("'", "")
                var path2 = args[2].replace("\"", "").replace("'", "")
                var path3 = args[3].replace("\"", "").replace("'", "")

                println("path1=$path1")
                println("path2=$path2")
                println("path3=$path3")
                copyDifferenceFile(path1,path2,path3)
            }

            "-e"->{
                var path1 = args[1].replace("\"", "").replace("'", "")
                var path2 = args[2].replace("\"", "").replace("'", "")
                var path3 = args[3].replace("\"", "").replace("'", "")
                var path4 = args[4].replace("\"", "").replace("'", "")
                var path5 = args[5].replace("\"", "").replace("'", "")
                var path6 = args[6].replace("\"", "").replace("'", "")

                println("path1=$path1")
                println("path2=$path2")
                println("path3=$path3")
                println("path4=$path4")
                println("path5=$path5")
                println("path6=$path6")
                PatchGenerate.generatePatch(path1,path2,path3,path4,path5,path6)
            }
            "-f"->{
                var path1 = args[1].replace("\"", "").replace("'", "")
                var path2 = args[2].replace("\"", "").replace("'", "")
                var path3 = args[3].replace("\"", "").replace("'", "")
                var path4 = args[4].replace("\"", "").replace("'", "")
                var path5 = args[5].replace("\"", "").replace("'", "")
                var path6 = args[6].replace("\"", "").replace("'", "")

                println("path1=$path1")
                println("path2=$path2")
                println("path3=$path3")
                println("path4=$path4")
                println("path5=$path5")
                println("path6=$path6")
                PatchGenerate.generatePatchNoResAssets(path1,path2,path3,path4,path5,path6)
            }

            "-g"->{
                var path1 = args[1].replace("\"", "").replace("'", "")
                var path2 = args[2].replace("\"", "").replace("'", "")
                var path3 = args[3].replace("\"", "").replace("'", "")
                var path4 = args[4].replace("\"", "").replace("'", "")
                var path5 = args[5].replace("\"", "").replace("'", "")

                println("path1=$path1")
                println("path2=$path2")
                println("path3=$path3")
                println("path4=$path4")
                println("path5=$path5")

                PatchGenerate.generateApkNoRes(path1,path2,path3,path4,path5)
            }


        }


    }



}


fun outHotUpdate(jsonPath: String, smaliPath: String, outPath: String) {
    var readText = readText(File(jsonPath))
    var gson = Gson()
    var fromJson = gson.fromJson(readText, ReplaceConfig::class.java)
    copyAndChangeFrameWorkPackageName(smaliPath, outPath, jsonPath)
    deleteSmaliForPackage(outPath, fromJson.retainPackage)
    deleteRSmali(outPath)
    println("更新包环境生成完成")
}

fun outPatch(jsonPath: String, oldSmali: String, newSmali: String, outPath: String) {
    if (File(outPath + "_temp").exists()) {
        println("删除$outPath _temp")
        File(outPath + "_temp").delete()
    }
    copyAndChangeFrameWorkPackageName(newSmali, outPath + "_temp", jsonPath)
    copyDifferenceFile(oldSmali, outPath + "_temp", outPath)
    deleteRSmali(outPath)
    println("补丁包环境生成完成")
}